//
//  HDCFFundSubscribeDetailVC.m
//  HaoDeCaiFu
//
//  Created by admin on 2017/12/18.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFFundSubscribeDetailVC.h"
#import "makeUpTableViewCell.h"
#import "HDCFBuyMakeUp.h"
#import "SVProgressHUD.h"
#import "HDCFLoginVc.h"
#import "HDCFOpenAccountVc.h"
#import "HDCFFundDetailVc.h"
#import "HDCFFundDetailFooter.h"//
#import "HDCFDetailFooterOne.h"//索引为2的section的cell
#import "UIImageView+WebCache.h"
#import "HDCFBuyInfoViewController.h"//购买信息填写vc
@interface HDCFFundSubscribeDetailVC ()
{
    UIButton *_lastBtn;
    UIButton *_lastFiveBtn;
    UIView   *_fiveBtnBottomLine;
    NSString *_textStr;
    
    UIButton * fundOrderBut;//开始预约按钮
    
    CGFloat cellTwoCount;//索引为1的区内cell数
    BOOL isSectionTwoHidden;
}
@property(nonatomic,strong)UIView *firstSectionHeaderView;
@property(nonatomic,strong)UIView *secondSectionHeaderView;
@property(nonatomic,strong)UILabel *thirdSectionHeaderView;
@property(nonatomic,strong)UIView *secondSectionfooterView;
@property(nonatomic,strong)UIView *clearHudView;

@property (nonatomic, strong) NSMutableArray * contentArray;
@property (nonatomic, strong) NSMutableArray * imagesArray;//Image的url的数组
@property (nonatomic, strong) NSMutableArray * imagesHeightArray;//Image的高度的数组
@property (nonatomic, strong) NSDictionary *makeUpData;

@end

static NSString * identiferThree = @"HDCFDetailFooterOnes";
@implementation HDCFFundSubscribeDetailVC

- (void)setPortfolioCode:(NSString *)portfolioCode{
    _portfolioCode = portfolioCode;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    cellTwoCount = 0;
    isSectionTwoHidden = YES;
    _contentArray = [NSMutableArray array];
    _imagesArray = [NSMutableArray array];
    _imagesHeightArray = [NSMutableArray array];
    self.title=@"基金详情";
    
    [self.tableView registerNib:[UINib nibWithNibName:@"makeUpTableViewCell" bundle:nil] forCellReuseIdentifier:@"makeUpTableViewCell"];
    //[self.tableView registerNib:[UINib nibWithNibName:@"HDCFDetailFooterOne" bundle:nil] forCellReuseIdentifier:identiferThree];
    
    //[self configerTableFooterView];
    
    fundOrderBut = [UIButton buttonWithType:UIButtonTypeCustom];
    fundOrderBut.frame = CGRectMake(0, screenHeight - 49-KNavHeight , screenWidth, 49);
    [fundOrderBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [fundOrderBut setTitle:@"开始预约" forState:UIControlStateNormal];
    [fundOrderBut addTarget:self action:@selector(jumpBuy) forControlEvents:UIControlEventTouchUpInside];
    [fundOrderBut setBackgroundColor:ZKcolor(184, 27, 36)];
    [self.view addSubview:fundOrderBut];
    
    [self getMakeUpDetailData];
}
-(void)getMakeUpDetailData
{
    [SVProgressHUD show];
    NSString * urlStr = @"api/Product";
    NSDictionary *paramet = @{@"Id":[NSString stringWithFormat:@"%ld",(long)_Id]};
    
    [HttpRequest sendSelfGetRequestWithURL:urlStr WithParamets:paramet success:^(id result) {
        NSString *code = [result[@"code"]stringValue];
        if ([code isEqualToString:@"1"]) {
            [SVProgressHUD dismiss];
            self.makeUpData = result[@"result"];
            //NSLog(@"%@",result);
            self.fundOrderModel = [HDCFFundOrderModel mj_objectWithKeyValues:result[@"result"]];
            
            if (self.fundOrderModel.State == 0) {
                [fundOrderBut setBackgroundColor:[UIColor lightGrayColor]];
                [fundOrderBut setTitle:@"即将开售" forState:UIControlStateNormal];
            }
            else if (self.fundOrderModel.State ==2)
            {
                [fundOrderBut setBackgroundColor:[UIColor lightGrayColor]];
                [fundOrderBut setTitle:@"已售罄" forState:UIControlStateNormal];
            }
            
            [_contentArray addObject:self.fundOrderModel.FundName?self.fundOrderModel.FundName:@""];
            [_contentArray addObject:self.fundOrderModel.FundCustodian?self.fundOrderModel.FundCustodian:@""];
            [_contentArray addObject:self.fundOrderModel.MinInitialIpoAmount?self.fundOrderModel.MinInitialIpoAmount:@""];
            [_contentArray addObject:self.fundOrderModel.EndDate?self.fundOrderModel.EndDate:@""];
            [_contentArray addObject:self.fundOrderModel.LaunchDate?self.fundOrderModel.LaunchDate:@""];
            [_contentArray addObject:self.fundOrderModel.InvestmentObjective?self.fundOrderModel.InvestmentObjective:@""];
            [_contentArray addObject:[NSString stringWithFormat:@"%.2f亿元",(long)self.fundOrderModel.LaunchSize/100000000.0]];
            [_contentArray addObject:[NSString stringWithFormat:@"%.1f元",(long)self.fundOrderModel.MinSubseqIpoAmount/1.0]];
            [_contentArray addObject:self.fundOrderModel.IpoStartDate?self.fundOrderModel.IpoStartDate:@""];
            
            
            NSString * imagesStr = self.fundOrderModel.Imgs;
            NSArray * imageArr = [self stringToJSON:imagesStr];
            [_imagesArray addObjectsFromArray:imageArr];
            //LLLog(@"%@",_imagesArray);
            for (int i = 0; i < _imagesArray.count; i++) {
                UIImageView * imageView = [[UIImageView alloc]init];
                [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@upload/product/%@",KSelfUrl,_imagesArray[i]]] placeholderImage:nil options:(SDWebImageRetryFailed) completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
                 {
                     if (!error) {
                         imageView.frame = CGRectMake(0, 0, screenWidth, image.size.height/image.size.width*screenWidth);
                         
                         [_imagesHeightArray addObject:[NSString stringWithFormat:@"%f",imageView.frame.size.height]];
                     }
                     
                     
                     if (_imagesArray.count == i+1)
                     {
                         //刷新对应的section
                         NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];
                         [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
                         return;
                     }
                 }];
            }
            
            //[self.tableView setTableHeaderView:[self configerTableHeader]];
            [self.tableView reloadData];
            
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"message"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
        
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}


-(UITableView*)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight-49-KNavHeight)];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview: _tableView];
    }
    return _tableView;
}


-(void)configerTableFooterView
{
    UIButton *btnTwoYF = [[UIButton alloc] init];
    btnTwoYF.frame = CGRectMake(0, 0, screenWidth, 50);
    [btnTwoYF setBackgroundColor:ZKcolor(245, 245, 245)];
    NSString *strleft = @"基金销售服务由奕丰金融提供\n-基金有风险 投资需谨慎-";
    NSMutableAttributedString *attrStrT = [[NSMutableAttributedString alloc] initWithString:strleft];
    [attrStrT addAttribute:NSForegroundColorAttributeName
                     value:[UIColor lightGrayColor]
                     range:NSMakeRange(0, strleft.length)];
    [attrStrT addAttribute:NSFontAttributeName
                     value:[UIFont boldSystemFontOfSize:12]
                     range:NSMakeRange(0, strleft.length)];
    [attrStrT addAttribute:NSFontAttributeName
                     value:[UIFont boldSystemFontOfSize:14]
                     range:NSMakeRange(7, 4)];
    [attrStrT addAttribute:NSForegroundColorAttributeName
                     value:ZKcolor(33, 33, 33)
                     range:NSMakeRange(7, 4)];
    [btnTwoYF setAttributedTitle:attrStrT forState:UIControlStateNormal];
    [btnTwoYF setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnTwoYF.titleLabel setTextAlignment:NSTextAlignmentCenter];
    btnTwoYF.titleLabel.lineBreakMode = 0;
    [btnTwoYF addTarget:self action:@selector(presentPage) forControlEvents:UIControlEventTouchUpInside];
    [self.tableView setTableFooterView:btnTwoYF];
    
}


- (void)presentPage{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *view = [[UIView alloc] init];
    self.clearHudView = view;
    view.frame = CGRectMake(0, 0, screenWidth, screenHeight);
    UITapGestureRecognizer *r5 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewHit)];
    r5.numberOfTapsRequired = 1;
    [view addGestureRecognizer:r5];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(30, (screenHeight - (screenWidth - 60) * 900.0 / 500.0) / 2, screenWidth - 60, (screenWidth - 60) * 900.0 / 500.0);
    imageView.image = [UIImage imageNamed:@"661511947049_.pic_hd.jpg"];
    [view addSubview:imageView];
    view.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1];
    [window addSubview:view];
}

- (void)viewHit{
    [self.clearHudView removeFromSuperview];
}

-(void)jumpBuy
{
    NSString *token = kToken;
    if (!(token.length > 0)) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录！" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            HDCFLoginVc *loginVc = [[HDCFLoginVc alloc] init];
            [self.navigationController pushViewController:loginVc animated:NO];
            
        }];
        UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction: action1];
        [alert addAction: action2];
        [self presentViewController:alert animated:NO completion:nil];
        return;
    }
    
//    NSString *openStatus = kOpenStatus;
//    if (!(openStatus.integerValue > 0)){
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先开户！" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            HDCFOpenAccountVc *loginVc = [[HDCFOpenAccountVc alloc] init];
//            [self.navigationController pushViewController:loginVc animated:NO];
//
//        }];
//        UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//
//        }];
//
//        [alert addAction: action1];
//        [alert addAction: action2];
//        [self presentViewController:alert animated:NO completion:nil];
//        return;
//    }
    
//    if (self.isAvalableBuy == NO) {
//        [SVProgressHUD showInfoWithStatus:@"此组合不存在,不能购买"];
//        [SVProgressHUD dismissWithDelay:1.0];
//        return;
//    }
    
    
    //0待开放 1预约销售中 2已售完
    if (self.fundOrderModel.State !=1) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"暂不能预约！" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction: action1];
        [self presentViewController:alert animated:NO completion:nil];
        return;
    }
    else
    {
        HDCFBuyInfoViewController * buyInfoVC = [HDCFBuyInfoViewController new];
        buyInfoVC.productId = _Id;
        [self.navigationController pushViewController:buyInfoVC  animated:YES];
    }
    
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"您将使用奕丰金融提供的基金销售服务" preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        HDCFBuyMakeUp*vc=[[HDCFBuyMakeUp alloc]init];
//        vc.dataDict = self.makeUpData;;
//        [self.navigationController pushViewController:vc animated:YES];
//    }];
//    UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//
//    }];
//    [alert addAction: action1];
//    [alert addAction: action2];
//    [self presentViewController:alert animated:NO completion:nil];
    
}
-(UIView*)configerTableHeader
{
    UILabel*headerLable=[[UILabel alloc]initWithFrame:CGRectMake(-1, 0, screenWidth+2, W_RATIO(100))];
    headerLable.text= [NSString stringWithFormat:@"  %@",_makeUpData[@"portfolioName"]];
    headerLable.layer.borderColor=[UIColor lightGrayColor].CGColor;
    headerLable.layer.borderWidth=0.6;
    headerLable.font=[UIFont systemFontOfSize:17];
    return headerLable;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark TableViewDatasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return cellTwoCount;
    }
    else if (section==2) {
        return _imagesArray.count;
    }
    else
    {
        return 1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        CGSize maxSize=CGSizeMake(screenWidth-20, 200);
        CGSize size=[self sizeWithFont:[UIFont systemFontOfSize:12] maxSize:maxSize string:self.fundOrderModel.Remark];
        
        return size.height+20;
    }
    else if (indexPath.section==1)
    {
        return 44;
    }
    else
    {
        if (_imagesHeightArray.count == 0) {
            return 0.0;
        }else
        {
            if (indexPath.row+1>_imagesHeightArray.count) {
                return 0.0;
            }
            float height = [_imagesHeightArray[indexPath.row] floatValue];
            return height;
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return W_RATIO(200);
    }
    if (section == 1) {
        return 0;
    }
    return 0;
}


-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return self.firstSectionHeaderView;
    }
    else if (section==1)
    {
        return self.secondSectionHeaderView;
    }
    else
    {
        return self.thirdSectionHeaderView;
    }
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section==1) {
        return self.secondSectionfooterView;
    }
    else
    {
        return nil;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==1) {
        return W_RATIO(100);
    }
    else
    {
        return 0;
    }
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier=@"Cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
    }
    
    static NSString *cellID = @"cellid";
    HDCFDetailFooterOne *cellTwo = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cellTwo) {
        cellTwo = [HDCFDetailFooterOne HDCFDetailFooterOne];
    }
    
    
    static NSString * cellIdentifie = @"CellIdentifierTwo";
    UITableViewCell * cellThree = [tableView dequeueReusableCellWithIdentifier:cellIdentifie];
    if (!cellThree)
    {
        cellThree=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifie];
        cellThree.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    
    if (indexPath.section == 1)
    {
        NSArray * typeLArray = @[@"基金名称",@"托管机构",@"认购门槛",@"产品期限",@"成立日期",@"投资领域",@"发行规模",@"追加金额",@"发行日期"];
        
        //HDCFDetailFooterOne * cellTwo = (HDCFDetailFooterOne *)[tableView dequeueReusableCellWithIdentifier:identiferThree];
        cellTwo.btnOneWidth.constant = screenWidth - 115;
        cellTwo.btnOne.titleLabel.lineBreakMode = 0;
        cellTwo.typeL.text = typeLArray[indexPath.row];
        [cellTwo.btnOne setTitle:_contentArray[indexPath.row] forState:UIControlStateNormal];
        return cellTwo;
    }
    else if (indexPath.section==0) {
        cell.textLabel.text = self.fundOrderModel.Remark;
        cell.textLabel.textColor = RGB(153, 153, 153);
        cell.textLabel.numberOfLines=0;
        cell.textLabel.font=[UIFont systemFontOfSize:12];
    }
    else if (indexPath.section==2)
    {
        UIImageView * imageView = [[UIImageView alloc]init];
        if (_imagesArray.count >0) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@upload/product/%@",KSelfUrl,_imagesArray[indexPath.row]]]];
        }
        if (_imagesHeightArray.count > 0) {
            if (indexPath.row +1 > _imagesHeightArray.count) {
                imageView.frame = CGRectMake(0, 0, 0, 0);
            }
            else
            {
                imageView.frame = CGRectMake(0, 0, screenWidth, [_imagesHeightArray[indexPath.row]floatValue]);
            }
        }
        [cellThree addSubview:imageView];
        return cellThree;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 2) {
        
        NSArray *fundArray = self.makeUpData[@"portfolioFund"];
        NSDictionary *dict = fundArray[indexPath.row];
        HDCFFundDetailVc *vc = [[HDCFFundDetailVc alloc] init];
        vc.dataDict = dict;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}

-(UIView*)firstSectionHeaderView
{
    NSString *agreeStr =[NSString stringWithFormat:@"基金类型：%@",self.fundOrderModel.FundType?self.fundOrderModel.FundType:@""];
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:agreeStr];
    
    NSString *shouyiStr =[NSString stringWithFormat:@"基金代码：%@",self.fundOrderModel.FundCode?self.fundOrderModel.FundCode:@""];
    
    NSString * funNameStr = [NSString stringWithFormat:@"%@",self.fundOrderModel.FundName?self.fundOrderModel.FundName:@""];
    
    if (!_firstSectionHeaderView) {
        _firstSectionHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, W_RATIO(200))];
        _firstSectionHeaderView.backgroundColor = [UIColor whiteColor];
        
        UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, screenWidth, W_RATIO(100))];
        titleLabel.tag = 333;
        titleLabel.text = funNameStr;
        titleLabel.font = [UIFont systemFontOfSize:20];
        [_firstSectionHeaderView addSubview:titleLabel];
        
        UILabel * lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, W_RATIO(100), screenWidth -20, 0.5)];
        lineLabel.backgroundColor = [UIColor lightGrayColor];
        [_firstSectionHeaderView addSubview:lineLabel];
        
        UILabel*leftLable=[[UILabel alloc]initWithFrame:CGRectMake(10, titleLabel.bottom, screenWidth/2-10,  W_RATIO(100))];
        leftLable.tag = 111;
        leftLable.attributedText=AttributedStr;
        leftLable.adjustsFontSizeToFitWidth = YES;
        [_firstSectionHeaderView addSubview:leftLable];
        
        UILabel*rightLable=[[UILabel alloc]initWithFrame:CGRectMake(screenWidth/2,titleLabel.bottom, screenWidth/2-10,  W_RATIO(100))];
        rightLable.tag=222;
        rightLable.text = shouyiStr;
        rightLable.textAlignment=2;
        rightLable.adjustsFontSizeToFitWidth = YES;
        [_firstSectionHeaderView addSubview:rightLable];
    }
    else
    {
        UILabel *leftLable=[_firstSectionHeaderView viewWithTag:111];
        leftLable.attributedText=AttributedStr;
        UILabel *rightLable=[_firstSectionHeaderView viewWithTag:222];
        rightLable.text = shouyiStr;
        UILabel *fundNameLabel=[_firstSectionHeaderView viewWithTag:333];
        fundNameLabel.text = funNameStr;
    }
    
    return _firstSectionHeaderView;
}

-(UIView*)secondSectionHeaderView
{
    if (!_secondSectionHeaderView) {
        _secondSectionHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, W_RATIO(100))];
        _secondSectionHeaderView.backgroundColor=[UIColor whiteColor];
        
//        UILabel * lineLabelOne = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, screenWidth -20, 0.5)];
//        lineLabelOne.backgroundColor = [UIColor lightGrayColor];
//        [_secondSectionHeaderView addSubview:lineLabelOne];
//
//        UILabel * lineLabelTwo = [[UILabel alloc]initWithFrame:CGRectMake(10, W_RATIO(100), screenWidth -20, 0.5)];
//        lineLabelTwo.backgroundColor = [UIColor lightGrayColor];
//        [_secondSectionHeaderView addSubview:lineLabelTwo];
//
//        UIButton * lookMoreBut = [[UIButton alloc]init];
//        lookMoreBut.frame = CGRectMake((screenWidth - 20-80)/2, (W_RATIO(100)-20)/2, 80, 20);
//        [lookMoreBut setTitle:@"查看更多" forState:UIControlStateNormal];
//        [lookMoreBut setTitleColor:RGB(153, 153, 153) forState:UIControlStateNormal];
//        lookMoreBut.titleLabel.font = [UIFont systemFontOfSize:16.0];
//        [_secondSectionHeaderView addSubview:lookMoreBut];
//
//
//        UIImageView * downImageView = [[UIImageView alloc]init];
//        downImageView.frame = CGRectMake(lookMoreBut.right, (W_RATIO(100)-20)/2, 20, 20);
//        downImageView.image = [UIImage imageNamed:@"common_down"];
//        [_secondSectionHeaderView addSubview:downImageView];
    }
    return _secondSectionHeaderView;
}

-(UILabel*)thirdSectionHeaderView
{
    if (!_thirdSectionHeaderView) {
        _thirdSectionHeaderView=[[UILabel alloc]initWithFrame:CGRectMake(-1, 0, screenWidth+2, W_RATIO(100))];
        _thirdSectionHeaderView.backgroundColor=[UIColor whiteColor];
        _thirdSectionHeaderView.textColor=[UIColor blackColor];
        _thirdSectionHeaderView.text=@"组合走势";
        _thirdSectionHeaderView.layer.borderColor=[UIColor lightGrayColor].CGColor;
        _thirdSectionHeaderView.layer.borderWidth=0.6;
        _thirdSectionHeaderView.font=[UIFont systemFontOfSize:15];
        _thirdSectionHeaderView.textAlignment=1;
        
    }
    return _thirdSectionHeaderView;
}

-(UIView*)secondSectionfooterView
{
    if (!_secondSectionfooterView) {
        _secondSectionfooterView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, W_RATIO(100))];
        _secondSectionfooterView.backgroundColor=[UIColor whiteColor];
        UILabel * lineLabelOne = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, screenWidth -20, 0.5)];
        lineLabelOne.backgroundColor = [UIColor lightGrayColor];
        [_secondSectionfooterView addSubview:lineLabelOne];
        
        UILabel * lineLabelTwo = [[UILabel alloc]initWithFrame:CGRectMake(10, W_RATIO(100), screenWidth -20, 0.5)];
        lineLabelTwo.backgroundColor = [UIColor lightGrayColor];
        [_secondSectionfooterView addSubview:lineLabelTwo];
        
//        UIButton * lookMoreBut = [[UIButton alloc]init];
//        lookMoreBut.frame = CGRectMake((screenWidth - 20-80)/2, (W_RATIO(100)-20)/2, 80, 20);
//        [lookMoreBut setTitle:@"查看更多" forState:UIControlStateNormal];
//        [lookMoreBut setTitleColor:RGB(153, 153, 153) forState:UIControlStateNormal];
//        lookMoreBut.titleLabel.font = [UIFont systemFontOfSize:16.0];
//        [lookMoreBut addTarget:self action:@selector(lookMoreButClick:) forControlEvents:UIControlEventTouchUpInside];
//        [_secondSectionfooterView addSubview:lookMoreBut];
//
//
//        UIImageView * downImageView = [[UIImageView alloc]init];
//        downImageView.frame = CGRectMake(lookMoreBut.right, (W_RATIO(100)-20)/2, 20, 20);
//        downImageView.image = [UIImage imageNamed:@"common_down"];
//        [_secondSectionfooterView addSubview:downImageView];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        btn.frame = CGRectMake(100, 6, screenWidth - 200, 30);
        [btn addTarget:self action:@selector(lookMoreButClick:) forControlEvents:UIControlEventTouchUpInside];
        if (cellTwoCount == 0) {
            CGSize size = [self sizeWithText:@"查看更多" font:[UIFont systemFontOfSize:14] maxW:MAXFLOAT];
            [btn setImage:nil forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:@"gengduo"] forState:UIControlStateNormal];
            [btn setTitle:nil forState:UIControlStateNormal];
            [btn setTitle:@"查看更多" forState:UIControlStateNormal];
            [btn setTitleColor:ZKcolor(170, 170, 170) forState:UIControlStateNormal];
            UIImage *image = [UIImage imageNamed:@"gengduo"];
            [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -image.size.width - 10, 0, image.size.width)];
            [btn setImageEdgeInsets:UIEdgeInsetsMake(0, size.width, 0, -size.width)];
        }else{
            CGSize size = [self sizeWithText:@"收起" font:[UIFont systemFontOfSize:14] maxW:MAXFLOAT];
            [btn setImage:nil forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:@"shouqi"] forState:UIControlStateNormal];
            [btn setTitle:nil forState:UIControlStateNormal];
            [btn setTitle:@"收起" forState:UIControlStateNormal];
            [btn setTitleColor:ZKcolor(170, 170, 170) forState:UIControlStateNormal];
            UIImage *image = [UIImage imageNamed:@"shouqi"];
            [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -image.size.width - 10, 0, image.size.width)];
            [btn setImageEdgeInsets:UIEdgeInsetsMake(0, size.width, 0, -size.width)];
        }
        [_secondSectionfooterView addSubview:btn];
    }
    return _secondSectionfooterView;
    
}
//
-(void)twoBtnClick:(UIButton*)btn
{

}
-(void)lookMoreButClick:(UIButton *)btn
{
    isSectionTwoHidden = !isSectionTwoHidden;
    if (isSectionTwoHidden == YES) {
        cellTwoCount = 0;
    }
    else
    {
        cellTwoCount = 9;
    }
    
    if (cellTwoCount == 0) {
        CGSize size = [self sizeWithText:@"查看更多" font:[UIFont systemFontOfSize:14] maxW:MAXFLOAT];
        [btn setImage:nil forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"gengduo"] forState:UIControlStateNormal];
        [btn setTitle:nil forState:UIControlStateNormal];
        [btn setTitle:@"查看更多" forState:UIControlStateNormal];
        [btn setTitleColor:ZKcolor(170, 170, 170) forState:UIControlStateNormal];
        UIImage *image = [UIImage imageNamed:@"gengduo"];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -image.size.width - 10, 0, image.size.width)];
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, size.width, 0, -size.width)];
    }else{
        CGSize size = [self sizeWithText:@"收起" font:[UIFont systemFontOfSize:14] maxW:MAXFLOAT];
        [btn setImage:nil forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"shouqi"] forState:UIControlStateNormal];
        [btn setTitle:nil forState:UIControlStateNormal];
        [btn setTitle:@"收起" forState:UIControlStateNormal];
        [btn setTitleColor:ZKcolor(170, 170, 170) forState:UIControlStateNormal];
        UIImage *image = [UIImage imageNamed:@"shouqi"];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -image.size.width - 10, 0, image.size.width)];
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, size.width, 0, -size.width)];
    }
    //一个section刷新
    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:1];
    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    NSIndexSet *indexSets=[[NSIndexSet alloc]initWithIndex:2];
    [self.tableView reloadSections:indexSets withRowAnimation:UITableViewRowAnimationAutomatic];
}
//计算字符串高度
-(CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize string:(NSString *)string{
    
    NSDictionary *attrs = @{NSFontAttributeName:font};
    
    return [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    
}
//计算高度
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxW:(CGFloat)maxW
{
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = font;
    
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
    
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

#pragma mark -
#pragma mark json转字典、数组
//json转数组
- (NSArray *)stringToJSON:(NSString *)jsonStr {
    if (jsonStr) {
        id tmp = [NSJSONSerialization JSONObjectWithData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments | NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:nil];
        
        if (tmp) {
            if ([tmp isKindOfClass:[NSArray class]]) {
                
                return tmp;
                
            } else if([tmp isKindOfClass:[NSString class]]
                      || [tmp isKindOfClass:[NSDictionary class]]) {
                
                return [NSArray arrayWithObject:tmp];
                
            } else {
                return nil;
            }
        } else {
            return nil;
        }
        
    } else {
        return nil;
    }
}
//json转字典
- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
@end
